package lelouet.sh3d.plugins.sweetDatacenter.gui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.rmi.RemoteException;

import javax.swing.BoxLayout;
import javax.swing.JComboBox;
import javax.swing.JPanel;

import lelouet.sh3d.plugins.sweetDatacenter.gui.SelectedElementPane.MControler;
import lelouet.sh3d.plugins.sweetDatacenter.model.GServer;

/**
 * This code was edited or generated using CloudGarden's Jigloo SWT/Swing GUI
 * Builder, which is free for non-commercial use. If Jigloo is being used
 * commercially (ie, by a corporation, company or business for any purpose
 * whatever) then you should purchase a license for each developer using Jigloo.
 * Please visit www.cloudgarden.com for details. Use of Jigloo implies
 * acceptance of these licensing terms. A COMMERCIAL LICENSE HAS NOT BEEN
 * PURCHASED FOR THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED LEGALLY FOR
 * ANY CORPORATE OR COMMERCIAL PURPOSE.
 */
public class ServerPropertiesPanel extends JPanel implements ActionListener {

	private static final long serialVersionUID = 1L;
	private JComboBox modelSelection;
	private ATextFieldValidation cpunum;
	private ATextFieldValidation cpucap;
	private ATextFieldValidation osdist;
	private ATextFieldValidation osfam;
	private ATextFieldValidation sizel;
	private ATextFieldValidation ip;
	private ATextFieldValidation mac;
	private ATextFieldValidation name;
	private ATextFieldValidation memcap;

	/** the controler to report modifications on */
	protected MControler controler;

	public ServerPropertiesPanel() {
		initGUI();
	}

	protected void setControler(MControler controler) {
		this.controler = controler;
	}

	public void updateServer(GServer toShow) {
		boolean withModel = toShow.getGridModel() != null;
		cpucap.setEnabled(!withModel);
		cpunum.setEnabled(!withModel);
		memcap.setEnabled(!withModel);
		sizel.setEnabled(!withModel);
		modelSelection.removeAllItems();
		for (String o : controler.getAvailableModels()) {
			modelSelection.addItem(o);
			try {
				if (toShow.getGridModel() != null
						&& toShow.getGridModel().getName().equals(o)) {
					modelSelection.setSelectedItem(o);
				}
			} catch (RemoteException e) {
				e.printStackTrace();
			}
		}
		cpucap.setText(Integer.toString(toShow.getCPUCapacity()));
		cpunum.setText(Integer.toString(toShow.getCPUNumber()));
		memcap.setText(Integer.toString(toShow.getMemoryCapacity()));
		sizel.setText(Integer.toString(toShow.getVSize()));
		osfam.setText(toShow.getOSFamily());
		osdist.setText(toShow.getOSDistrib());
		name.setText(toShow.getName());
		ip.setText(toShow.getIP());
		mac.setText(toShow.getMac());
	}

	protected static final int FIELD_WIDTH = 170;
	protected static final int LABEL_WIDTH = 120;
	protected static final int ROW_HEIGHT = 20;

	@SuppressWarnings("serial")
	private void initGUI() {
		BoxLayout thisLayout = new BoxLayout(this, BoxLayout.Y_AXIS);
		setLayout(thisLayout);
		name = new ATextFieldValidation("hostname : ", LABEL_WIDTH,
				FIELD_WIDTH, ROW_HEIGHT) {

			@Override
			public void onValidation(String text) {
				controler.setName(text);
			}
		};
		add(name);
		cpunum = new ATextFieldValidation("CPU Number : ", LABEL_WIDTH,
				FIELD_WIDTH, ROW_HEIGHT) {

			@Override
			public void onValidation(String text) {
				controler.setCPUNumber(text);
			}
		};
		add(cpunum);
		cpucap = new ATextFieldValidation("CPU capacity : ", LABEL_WIDTH,
				FIELD_WIDTH, ROW_HEIGHT) {

			@Override
			public void onValidation(String text) {
				controler.setCPUCapacity(text);
			}
		};
		add(cpucap);
		memcap = new ATextFieldValidation("memory capacity : ", LABEL_WIDTH,
				FIELD_WIDTH, 20) {

			@Override
			public void onValidation(String text) {
				controler.setMemoryCapacity(text);
			}
		};
		add(memcap);
		sizel = new ATextFieldValidation("size in U : ", LABEL_WIDTH,
				FIELD_WIDTH, ROW_HEIGHT) {

			@Override
			public void onValidation(String text) {
				controler.setVSize(text);
			}
		};
		add(sizel);
		osfam = new ATextFieldValidation("OS family : ", LABEL_WIDTH,
				FIELD_WIDTH, ROW_HEIGHT) {

			@Override
			public void onValidation(String text) {
				controler.setOsFamily(text);
			}
		};
		add(osfam);
		osdist = new ATextFieldValidation("OS distribution : ", LABEL_WIDTH,
				FIELD_WIDTH, ROW_HEIGHT) {

			@Override
			public void onValidation(String text) {
				controler.setOsDistrib(text);
			}
		};
		add(osdist);
		ip = new ATextFieldValidation("IP address : ", LABEL_WIDTH,
				FIELD_WIDTH, ROW_HEIGHT) {

			@Override
			public void onValidation(String text) {
				controler.setServerIp(text);
			}
		};
		add(ip);
		mac = new ATextFieldValidation("mac address : ", LABEL_WIDTH,
				FIELD_WIDTH, ROW_HEIGHT) {

			@Override
			public void onValidation(String text) {
				controler.setServerMac(text);
			}
		};
		add(mac);
		modelSelection = new JComboBox();
		modelSelection.setEditable(false);
		modelSelection.addActionListener(this);
		add(modelSelection);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == modelSelection) {
			controler.setServerModel((String) modelSelection.getSelectedItem());
		}
	}
}
